//
//  LYChooseListView.m
//  artist
//
//  Created by Jay_Apple on 16/4/2.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYChooseListView.h"
#import "LYSubjectListDBTool.h"
#import "LYSubDataModel.h"
#import "LYChooseMenuCell.h"
#import "LYSubsCell.h"


@interface LYChooseListView ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) NSArray *menuArray;


@property (strong, nonatomic) NSArray *contentArray;
@property (weak, nonatomic) IBOutlet UIView *bottomBar;

@property (nonatomic, weak) LYSubDataModel *selectedModel;

@property (nonatomic, copy) NSString *selectedSub;

@property (nonatomic, assign) listMode mode;

@end

static NSString * const collectioncellID = @"LYSubsCell";

@implementation LYChooseListView


+ (instancetype)showOnView:(UIView *)view WithMode:(listMode)mode{


    LYChooseListView *chooseView = [LYChooseListView viewForNib];
    
    chooseView.mode = mode;
    
    if ([view isKindOfClass:[UIScrollView class]]) {
        chooseView.frame = view.frame;
        [view.superview addSubview:chooseView];
    }else{
        chooseView.frame = view.bounds;
        [view addSubview:chooseView];
    }
    
    return chooseView;


}


+ (instancetype)showOnView:(UIView *)view{
    
    return [self showOnView:view WithMode:listModeSingleSelection];
    
}



- (void)awakeFromNib{

    [super awakeFromNib];
    
    //强制布局
    [self layoutIfNeeded];
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LYSubsCell class]) bundle:nil]  forCellWithReuseIdentifier:collectioncellID];
    
    //获取科目数据
    self.menuArray = [LYSubjectListDBTool getSubjectData];
    
    if (self.menuArray.count > 0) {
        //默认选中第一个
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    }
    
    //底部条
    self.bottomBar.layer.borderWidth = 1;
    self.bottomBar.layer.borderColor = [UIColor colorWithHexString:@"#DDDDDD" alpha:1].CGColor;
    

}

#pragma mark - 重写方法
- (void)setContentArray:(NSArray *)contentArray{

    _contentArray = contentArray;
    
    [self.collectionView reloadData];
}

- (void)setSelectedModel:(LYSubDataModel *)selectedModel{

    _selectedModel = selectedModel;
    
    self.contentArray = selectedModel.subjects;

}

- (void)setMode:(listMode)mode{

    _mode = mode;
    
    switch (_mode) {
        case listModeNormal:
            
            self.backgroundColor = [UIColor clearColor];
            
            break;
            
        case listModeSingleSelection:
            
            self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
            
            break;
    }

}

#pragma mark - tableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.menuArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *menucellID = @"LYChooseMenuCell";
    
    LYChooseMenuCell * cell = [LYChooseMenuCell cellWithTableView:tableView andIndetifier:menucellID];
    LYSubDataModel *model = self.menuArray[indexPath.row];
    
    cell.model = model;
    return cell;
}


#pragma mark - tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYSubDataModel *model = self.menuArray[indexPath.row];
    
    if (self.selectedModel == model) {
        return;
    }else{
    
        self.selectedModel.selected = NO;
        model.selected = YES;
        self.selectedModel = model;
        [self.tableView reloadData];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 35;
}

#pragma mark - collectionViewDelegate

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%f",self.collectionView.My_Width);
    
    return CGSizeMake(self.collectionView.My_Width * 0.5 - 25, 30);
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    self.selectedSub = self.contentArray[indexPath.item];
    
    if (self.block) {
        self.block(self.selectedSub);
    }

    if (self.mode == listModeSingleSelection) {
        [self dismiss];
    }
    
}

#pragma mark - collectionViewDatasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.contentArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LYSubsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectioncellID forIndexPath:indexPath];
    NSString *title = self.contentArray[indexPath.item];
    cell.title = title;
    return cell;
}

- (void)dismiss{
    
    
    [self removeFromSuperview];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    if (self.mode == listModeSingleSelection) {
        [self dismiss];
    }
    
    
}
@end
